package cn.tedu.cgb2110boot02.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/re/RequestServlet01")
public class RequestServlet01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("恭喜您，请求成功！");
        //http://localhost:8080/re/RequestServlet01?name=jack&pwd=123&like=足球&like=篮球
        //Servlet解析请求参数
        //1.getParameter()--根据参数名获取参数值
        String s = request.getParameter("name");
        System.out.println(s);
        String p = request.getParameter("pwd");
        System.out.println(p);
        //2.getParameterValues()--根据参数名获取参数值,可以获取多个值，存入数组
        String[] s1 = request.getParameterValues("like");
//        System.out.println(s1.toString());//打印的是地址值，不能空指针
        System.out.println(Arrays.toString(s1));//[足球,篮球]
        //3.其他API
        String s2 = request.getCharacterEncoding();//获取请求使用的字符集UTF-8
        System.out.println(s2);
        String method = request.getMethod();//获取请求方法 get
        System.out.println(method);
        String path = request.getContextPath();//获取项目名称
        System.out.println(path);
        String uri = request.getRequestURI();//要访问的资源路径
        System.out.println(uri);
        StringBuffer url = request.getRequestURL();//要访问的全路径
        System.out.println(url);
        int remotePort = request.getRemotePort();//发起请求时网页的端口号
        System.out.println(remotePort);
        String string = request.getQueryString();//请求参数的编码
        System.out.println(string);
    }
}
